<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1{
            width: 200px;
            height: 200px;
            background-color: blanchedalmond;
        }
        #sp{
            background-color: red;
        }
    </style>
</head>
<script>
    /**
     * Bubble
     * 事件冒泡 
     * 所谓冒泡就是事件向上传导，当后代元素被触发时，其祖先元素的相同事件也会被触发
     * 在开发中大部分冒泡都是有用的，如果不想发生事件冒泡可以使用事件对象来取消冒泡
     * */
    window.onload = function () {
        var sp = document.getElementById("sp");
        var box1 = document.getElementById("box1");
        //为span绑定一个点击响应函数
        sp.onclick = function (event) {
            event  = window.event || event;
            alert("span")
            //取消事件冒泡
            event.cancelBubble = true;
        };
        //为box1绑定一个点击响应函数
        box1.onclick = function (event) {
            event  = window.event || event;
            alert("div")
            event.cancelBubble = true;
        };
        //为body绑定点击响应函数
        var body = document.body;
        body.onclick = function (event) {
            event  = window.event || event;
            alert("body")
            event.cancelBubble = true;
        };
    }
</script>
<body>
    <div id="box1">
        我是box1 <span id="sp">我是span</span>
    </div>
</body>
</html>